<?php

/**
 * Используется для получения списка устанавливаемых прав доступа
 * при создании или модификации записи
 */

class RL_ItemRightsLists {
	private $aa;

	private $level = array(
	0 => "персонально автор",
	1 => "автор и модераторы",
	2 => "автор и участники",
	3 => "автор и авторизованные",
	4 => "доступно всем",
	);

	private $roles = array(
	'anonymous'=>0,
	'registered'=>0,
	'member'=>0,
	'moderator'=>0,
	);

	private $rights = array();
	private $owner_oid = 0;

	public function __construct( $aa ) {
		$this->aa = $aa;

		$roles = $aa->getRawRolesList();

		foreach ( array_keys($this->roles) as $role ) {
			if ( isset($roles[$role]) && !empty($roles[$role]) ) {
				$this->roles[$role] = $roles[$role];
			}
		}
	}

	public function getViewItemRights( $readItemLevel = 0, $createCommentLevel = 0,
	$owner_oid ) {
		$this->owner_oid = $owner_oid;

		if ( !isset($this->level[$readItemLevel])
		|| !isset($this->level[$createCommentLevel]) ) {
			return array();
		}

		$this->makeRightsArray();
		$this->fillItemReadSlice($readItemLevel);
		$this->fillCommentCreateSlice($createCommentLevel);

		$result = $this->getOwnerRights($readItemLevel, $createCommentLevel)
		+
		$this->getModeratorRightsForItem(
		$this->rights['moderator']['item_read'],
		$this->rights['moderator']['comment_create']
		)
		+ $this->getMemberRightsForItem(
		$this->rights['member']['item_read'],
		$this->rights['member']['comment_create']
		)
		+ $this->getRegisteredRightsForItem(
		$this->rights['registered']['item_read'],
		$this->rights['registered']['comment_create']
		)
		+ $this->getAnonymousRightsForItem(
		$this->rights['anonymous']['item_read'],
		$this->rights['anonymous']['comment_create']
		);

		return $result;
	}


	private function getOwnerRights($readItemLevel, $createCommentLevel) {
		$result = array();
		if ( 0 == $readItemLevel ) {
			$result['item_read'] = 1;
			$result['item_edit'] = 1;
			$result['item_delete'] = 1;
		}
		if ( 0 == $createCommentLevel ) {
			$result['comment_create'] = 1;
		}

		return array( $this->owner_oid => $result );
	}

	private function getModeratorRightsForItem( $readItem = 0, $createComment = 0 ) {
		$rights = array( 'item_read'=>$readItem, 'comment_create'=>$createComment );
		if ( 0 == $readItem ) {
			$rights['item_edit'] = 0;
		}
		return array( $this->roles['moderator'] => $rights );
	}

	private function getMemberRightsForItem( $readItem = 0, $createComment = 0 ) {
		return array( $this->roles['member'] =>
		array('item_read'=>$readItem, 'comment_create'=>$createComment) );
	}

	private function getRegisteredRightsForItem( $readItem = 0, $createComment = 0 ) {
		return array( $this->roles['registered'] =>
		array('item_read'=>$readItem, 'comment_create'=>$createComment) );
	}

	private function getAnonymousRightsForItem( $readItem = 0, $createComment = 0 ) {
		return array( $this->roles['anonymous'] =>
		array('item_read'=>$readItem, 'comment_create'=>$createComment) );
	}

	private function makeRightsArray() {
		foreach (array_keys($this->roles) as $role) {
			$this->rights[$role] = array( 'item_read'=>1, 'comment_create'=>1 );
		}
	}

	private function fillItemReadSlice($readItemLevel) {
		if ($readItemLevel < 4) {
			$this->rights['anonymous']['item_read'] = 0;
		}
		if ($readItemLevel < 3) {
			$this->rights['registered']['item_read'] = 0;
		}
		if ($readItemLevel < 2) {
			$this->rights['member']['item_read'] = 0;
		}
		if ($readItemLevel < 1) {
			$this->rights['moderator']['item_read'] = 0;
		}
	}

	private function fillCommentCreateSlice($createCommentLevel) {
		if ($createCommentLevel < 4) {
			$this->rights['anonymous']['comment_create'] = 0;
		}
		if ($createCommentLevel < 3) {
			$this->rights['registered']['comment_create'] = 0;
		}
		if ($createCommentLevel < 2) {
			$this->rights['member']['comment_create'] = 0;
		}
		if ($createCommentLevel < 1) {
			$this->rights['moderator']['comment_create'] = 0;
		}
	}
}